package io.leo.leoutils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class DisplayUtils {

    public static int dp2Px(Context context,int dps) {
        return Math.round(context.getResources().getDisplayMetrics().density * dps);
    }

    /**
     * 截取scrollview的屏幕
     * **/
    public static Bitmap getBitmapByView(int color,String path,ScrollView scrollView) throws IOException {
        int h = 0;
        Bitmap bitmap = null;
        // 获取listView实际高度
        for (int i = 0; i < scrollView.getChildCount(); i++) {
            h += scrollView.getChildAt(i).getHeight();
        }

        Paint backPaint = new Paint();
        backPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        backPaint.setColor(color);
        backPaint.setAntiAlias(true);
        // 创建对应大小的bitmap
        bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
                Bitmap.Config.RGB_565);
        final Canvas canvas = new Canvas(bitmap);
        canvas.drawRect(0,0,scrollView.getWidth(),h,backPaint);
        scrollView.draw(canvas);
        // 测试输出
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(path);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (null != out) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        }

        return bitmap;
    }


}
